// 根据传入的参数，返回url中的参数值
export const getUrlParams = (searchParams: URLSearchParams, param: string) => {
    return searchParams.get(param) || ''
}

// 获取所有url参数
export const getQueryParams = (searchParams: URLSearchParams) => {
    const queryParams = Object.fromEntries(searchParams)
    for (const key in queryParams) {
        if (key === 'channel' || key === 'network') {
            // 统一转换为首字母大写
            const normalizedKey = key.charAt(0).toUpperCase() + key.slice(1).toLowerCase()
            queryParams[normalizedKey] = queryParams[key]
            // 删除小写参数
            delete queryParams[key]
        } else {
            queryParams[key] = queryParams[key]
        }
    }
    return queryParams
}

// 获取当前url的参数，不包含name,...参数
export const homeParams = (searchParams: URLSearchParams) => {
    const queryParams = Object.fromEntries(searchParams)
    delete queryParams.name
    delete queryParams.category
    delete queryParams.subCategory
    return queryParams
}

/**
 * DIY输出log日志
 * @param value 被输出的日志
 */
export const diyConsole = (value: string = "") => {
    console.log("%c%s", "background:#00dc82;padding:2px 8px;border-radius:2px;color:black", value);
  };
  
  /**
   * 在运行时分析CPU和内存使用，避免内存溢出和CPU占用过高
   * @description 开发环境下运行，生产环境下不使用
   */
  export const initCpu = async () => {
    // 根据自己项目在生产环境是否需要也监控
    const { isShowCPU } = useRuntimeConfig().public;
    
    if (Number(isShowCPU) === 0) return;
    const data = await $fetch<NodeCpuType>("/api/getCPU", {
      method: "POST",
      body: {
        hostName: useHostname(),
      },
    });
    const { cpu, memory } = data || {};
    diyConsole(cpu);
    diyConsole(memory);
  };

  export type NodeCpuType = {
    cpu: string;
    memory: string;
  };